package ui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JOptionPane;

import dao.TeacherDao;
import dao.TeacherDaoJDBCImpl;
import model.Student;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author qiaopeng
 */
public class SearchBynameFrame extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
     * Creates new form NewJFrame1
     */
    public SearchBynameFrame() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
   
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
               
            },
            new String [] {
                
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("确定");
        getRootPane().setDefaultButton(jButton1);//设置回车响应
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jLabel1.setText("输入学生姓名（关键字）：");

        jTextField1.setText("");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(111, 111, 111)
                .addComponent(jLabel1)
                .addGap(41, 41, 41)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 250, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(148, 148, 148))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(25, 25, 25)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 553, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    	String stuname = jTextField1.getText();
    	TeacherDao th = new TeacherDaoJDBCImpl();
    	List<Student> stu = new ArrayList<>();
    	stu = th.searchByname(stuname);
    	/*for(int i = 0;i < stu.size();i++) {
    		System.out.println(stu.get(i).toString()+" "+stu.get(i).getSublist().get(0).toString());
    	}*/
    	if(stu.size()==0) {
    		JOptionPane.showMessageDialog(null,"对不起，查无此人");
    	}else {
	    	int colum = 5+stu.get(0).getSublist().size();
	    	int row = stu.size();
	    	Object [][] object = new Object [row][colum];
	    	String [] s = new String[colum];
	    	s[0] = "学号";s[1] = "姓名";s[2] = "班级";s[3] = "性别";s[4] = "出生日期";
	    	for(int k =0;k<stu.get(0).getSublist().size();k++) {
	    		s[k+5] = stu.get(0).getSublist().get(k).getName();
	    	}
	    	for(int i = 0 ;i < row;i++) {
	    			object[i][0] = stu.get(i).getStudentID();
	    			object[i][1] = stu.get(i).getName();
	    			object[i][2] = stu.get(i).getClasses();
	    			object[i][3] = stu.get(i).getGender();
	    			object[i][4] = stu.get(i).getBirthdate();
	    			int r=0;
	    			for(int j = 5;j<colum;j++,r++) {
	    				if(stu.get(i).getSublist().get(r).getScore()==-1) {
	    					object[i][j] = "未选";
	    				}
	    				else {
	    					object[i][j] = stu.get(i).getSublist().get(r).getScore();
	    				}
	    			}
	    		
	    	}
	    	
	    	for(int i = 0;i<row;i++) {
	    		for(int j =0;j<colum;j++) {
	    			System.out.print(object[i][j]);
	    		}
	    		System.out.println();
	    	}
	    	jTable1.setModel(new javax.swing.table.DefaultTableModel(
	                object,
	                s
	            ));
	       jScrollPane1.setViewportView(jTable1);
    	}
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}

